package org.noote.spidy.core;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.view.View;

public class VisualMessage_View extends View {
	
	private static final int _GRID_CELL_X = 8;
	private static final int _GRID_CELL_Y = 8;
	
	public static final int MESSAGE_VISUAL_VOID = 0;
	public static final int MESSAGE_VISUAL_ERROR = 1;
	public static final int MESSAGE_VISUAL_WAIT_CONNECT = 2;
	public static final int MESSAGE_VISUAL_OK = 3;
	
	public Paint _black = new Paint();
	public Paint _white = new Paint();
	public Paint _red = new Paint();
	public Paint _green = new Paint();
	public Paint _blue = new Paint();

	// grid size
	private int _iGridBeginX=0, _iGridBeginY=0;
	private int _iGridCellSize=0;
	private int _iRectRound=5, _iRectSpace=2;
	
	private int _iAnim = 0;
	private float _fAnimTime = 0;
	private int _iAnimStep = 50;
	
	// prealloc :)
	RectF rectF = new RectF();
	
	private Handler myHandler = new Handler();
	private Runnable myRunnable = new Runnable() {
		@Override
		public void run() {
				
			// 	force redraw :)	
			postInvalidate();
			
			// anim step !
			_fAnimTime += _iAnimStep/1000.0f;
	 
	    	myHandler.postDelayed(this,_iAnimStep);
	    }
	};

	public VisualMessage_View(Context context) {
		super(context);
		
		_black.setARGB(255, 0, 0, 0);
		_white.setARGB(255, 255, 255, 255);
		_red.setARGB(255, 255, 0, 0);
		_green.setARGB(255, 0, 255, 0);
		_blue.setARGB(255, 0, 0, 255);
		
		create(_GRID_CELL_X, _GRID_CELL_Y);
	}

	private boolean create(int iCols, int iRows) {
		
		myHandler.postDelayed(myRunnable,_iAnimStep);
		
		return true;
	}
	
	private boolean _calculAeras(Canvas canvas)
	{
		int iX = canvas.getWidth();
		int iY = canvas.getHeight();
		
		if(iX == 0 || iY == 0)
		{
			_iGridCellSize = 0;
			return false;
		}
		
		if(iY>iX) // larger than higher
			_iGridCellSize = iX/_GRID_CELL_X;
		else
			_iGridCellSize = iY/_GRID_CELL_Y;

		if(_iGridCellSize>0 && _iGridCellSize%2 == 0) // better circle looking with unpeer size ;) 
			_iGridCellSize--;
		
		_iGridBeginX = (iX-(_iGridCellSize*_GRID_CELL_X))/2;
		_iGridBeginY = (iY-(_iGridCellSize*_GRID_CELL_Y))/2;
		
		if(_iGridCellSize>15)
			_iRectSpace = 2;
		else
			_iRectSpace = 1;
		if(_iGridCellSize+_iRectSpace>10)
			_iRectRound=5;
		else if(_iGridCellSize+_iRectSpace>5)
			_iRectRound=3;
		else _iRectRound=1;
		
		return true;
	}
	
	@Override
	protected void onSizeChanged(int width, int height, int width_old, int height_old)
	{
		super.onSizeChanged(width, height, width_old, height_old);

		_iGridCellSize = 0;
	}
    
	@Override
    public void onDraw(Canvas canvas) {
		if(_iGridCellSize == 0)
		{
			if(!_calculAeras(canvas)) return;
		}

		if(_iAnim == MESSAGE_VISUAL_ERROR)
		{
			int r=0, g=0, b=0;
			r += (_fAnimTime%1.0f)*255;
			canvas.drawRGB(r, g, b);
		}
		else if(_iAnim == MESSAGE_VISUAL_WAIT_CONNECT)
		{
			int r=0, g=0, b=0;
			g += (_fAnimTime%1.0f)*255;
			canvas.drawRGB(r, g, b);
		}
		else if(_iAnim == MESSAGE_VISUAL_VOID)
		{
			canvas.drawRGB(64, 64, 64);
		}
		else
		{
			canvas.drawRGB(0, 0, 0);
		}
		
		for(int y=0; y<_GRID_CELL_Y; y++)
		{
			for(int x=0; x<_GRID_CELL_X; x++)
			{
				rectF.left = _iGridBeginX+_iGridCellSize*x+_iRectSpace;
				rectF.top = _iGridBeginY+_iGridCellSize*y+_iRectSpace;
				
				rectF.right = rectF.left +_iGridCellSize-_iRectSpace*2;
				rectF.bottom = rectF.top +_iGridCellSize-_iRectSpace*2;

				if(_iAnim == MESSAGE_VISUAL_ERROR)
				{
					canvas.drawRoundRect(rectF, _iRectRound, _iRectRound, _red);
				}
				else if(_iAnim == MESSAGE_VISUAL_WAIT_CONNECT)
				{
					canvas.drawRoundRect(rectF, _iRectRound, _iRectRound, _black);
				}
				else if(_iAnim == MESSAGE_VISUAL_OK)
				{
					canvas.drawRoundRect(rectF, _iRectRound, _iRectRound, _green);
				}
			}
		}
    }
	
	public void enableMessage(int iMsg) {
		switch(iMsg)
		{
		case MESSAGE_VISUAL_ERROR:
		{
			_iAnim = iMsg;
			_fAnimTime = 0;			
		}
		break;
		case MESSAGE_VISUAL_WAIT_CONNECT:
		{
			_iAnim = iMsg;
			_fAnimTime = 0;			
		}
		break;
		case MESSAGE_VISUAL_OK:
		{
			_iAnim = iMsg;
			_fAnimTime = 0;			
		}
		break;
		default:
		{
			_iAnim = MESSAGE_VISUAL_VOID;
			_fAnimTime = 0;
		}
			break;
		}
	}
}
